<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>编辑成绩</title>
    <link rel="stylesheet" type="text/css"
          href="assets/admin/layui/css/layui.css"/>
    <link rel="stylesheet" type="text/css" href="assets/admin/css/admin.css"/>
</head>

<body>
<div class="page-content-wrap" id="scoreUpdate">

    <form class="layui-form" action="" id="">

        <div class="layui-form-item" style="display: none;">
            <label class="layui-form-label">成绩编号</label>
            <div class="layui-input-block">
                <input type="text" name="id" id="id" placeholder="请输入编号" autocomplete="off"
                       class="layui-input">
                <input type="hidden" name="myStuId" id="stuIdHidden" value="">
                <input type="hidden" name="mySubId" id="subIdHidden" value="">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">请选择学生</label>
            <div class="layui-input-block">
                <select name="stuId" id="stuId" lay-verify="required" lay-search="">
                    <option value="">请选择学生</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">科目</label>
            <div class="layui-input-block">
                <select name="subId" id="subId" lay-verify="required">
                    <option value="">请选择科目</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">分数</label>
            <div class="layui-input-block">
                <input type="text" name="score" id="score" lay-verify="scoreValue"
                       placeholder="请输入分数" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item m-login-btn" style="text-align:center">
            <div class="layui-inline">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="updateScore">修改成绩</button>
            </div>
            <div class="layui-inline">
                <button type="reset" class="layui-btn layui-btn-primary  site-demo-active"
                        data-type="reset">取消添加
                </button>
            </div>
        </div>
    </form>

    <hr></hr>
    <h5 style="text-align: center;">@学生成绩管理系统</h5>
</div>
<script src="assets/js/jquery-3.3.1.min.js"></script>
<script src="assets/admin/js/login-interceptor.js"></script>
<script src="assets/admin/layui/layui.all.js" type="text/javascript"
        charset="utf-8"></script>
<script>

    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer, layedit = layui.layedit;
        var $ = layui.jquery;

        //加载下拉框

        $.ajax({
            //几个参数需要注意一下
            type: "POST",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "/student/studentSelect",//url
            data: {},
            success: function (result) {
                if (result.code == "0") {
                    var data = result.data;
                    $("#stuId").html('<option value="">请输入/选择学生</option>');
                    if (data.length > 0) {
                        var editStuId = $("#stuIdHidden").val();
                        for (i = 0; i < data.length; i++) {
                            var temp = data[i];
                            if (editStuId == temp.studentNo) {
                                $("#stuId").append('<option selected value="' + temp.studentNo + '" selected>' + temp.studentName + '</option>');
                            } else {

                                $("#stuId").append('<option value="' + temp.studentNo + '">' + temp.studentName + '</option>');
                            }
                        }
                        layui.form.render('select');
                    }

                } else if (result.code == "1") {
                    layer.msg('加载学生下拉框失败', {
                        icon: 5
                    });
                } else {
                    layer.msg('加载学生下拉框失败', {
                        icon: 5
                    });
                }
            },
            error: function () {
                layer.msg('加载学生下拉框失败', {
                    icon: 5
                });
            }
        });

        $.ajax({
            type: "POST",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "/subjects/subjectSelect",//科目数据接口URL，需要根据实际情况修改
            data: {},
            success: function (result) {
                if (result.code == "0") {
                    var data = result.data;
                    $("#subId").html('<option value="">请选择科目</option>');
                    if (data.length > 0) {
                        var editSubId = $("#subIdHidden").val();
                        for (i = 0; i < data.length; i++) {
                            var subject = data[i];
                            if(editSubId == subject.id) {
                                $("#subId").append('<option selected value="' + subject.id + '" selected>' + subject.name + '</option>');
                            } else {
                                $("#subId").append('<option value="' + subject.id + '">' + subject.name + '</option>');
                            }
                        }
                        form.render('select'); //重新渲染下拉框
                    }
                } else {
                    layer.msg('加载科目下拉框失败', {
                        icon: 5
                    });
                }
            },
            error: function () {
                layer.msg('加载科目下拉框失败', {
                    icon: 5
                });
            }
        });


        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');
        //自定义验证规则
        form.verify({
            score: [
                /^100$|^(\d|[1-9]\d)$/,
                '0-100之间的正整数'
            ]
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });
        form.on('submit(updateScore)', function (data) {
            var str = JSON.stringify(data.field)
            var param = JSON.parse(str);
            $.ajax({
                //几个参数需要注意一下
                type: "POST",//方法类型
                dataType: "json",//预期服务器返回的数据类型
                url: "/score/updateScore",//url
                data: param,
                success: function (result) {
                    if (result.code == "0") {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index);
                        parent.layer.msg('修改成绩成功');
                        parent.layui.table.reload('scoreTest', {page: {curr: 1}});
                    } else {
                        layer.msg('修改成绩失败', {
                            icon: 5
                        });
                    }
                },
                error: function () {
                    layer.msg('服务器错误', {
                        icon: 5
                    });
                }
            });

            return false;
        });
        //触发事件
        var active = {
            reset: function () {
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
                parent.layer.msg('已取消操作');
            }
        };
        $('#scoreUpdate .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
</body>

</html>